<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><router-link to="/reporting/dayInquiries">日报查询</router-link></el-breadcrumb-item>
        <el-breadcrumb-item>编辑日报</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <basic-container>
      <el-form :model="categoryForm" label-width="90px" label-position="right">
        <el-row>
          <el-col :span="8">
            <el-form-item label="汇报人" required>
              <el-select 
                filterable
                remote 
                :remote-method="remoteStaffList"
                class="fill-width" v-model="categoryForm.reportManId" placeholder="请选择">
                <el-option
                  v-for="item in staffList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="汇报类型" required>
                <el-select class="fill-width" v-model="categoryForm.reportType" placeholder="请选择">
                <el-option
                  v-for="item in reportTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="汇报内容" required>
                <el-input v-model="categoryForm.reportContent" placeholder="请输入" type="textarea" maxlength="500"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="标注位置">
              <p style="text-align: left">{{position.formattedAddress}}</p>
              <avue-map @input="changePosition" v-model="position" style="width:200px" :lnglat="lnglat"></avue-map>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="汇报地址">
                <el-input v-model="categoryForm.reportAddress" placeholder="请输入" type="textarea" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="图片列表">
              <div v-if="categoryForm.photoList" class="thumbUrl" v-for="(item,index) in categoryForm.photoList">
                <img :src="item.picUrl" />
                <div class="hover_show">
                  <i class="el-icon-delete" @click="handlePictureRemove(index)"></i>
                </div>
              </div>
              <div class="select_img" @click="sel_img()" style="float: left;"><i class="el-icon-plus"></i></div>
              <div v-if="sleImg">
                <el-dialog title="选择图片" :visible.sync="sleImg" style="text-align: left;">
                  <sel-img-photogallery ref="selImg" type="11" @select="onSelectImgs"></sel-img-photogallery>
                </el-dialog>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <div>
            <el-button type="primary" size="medium" @click="save">保存</el-button>
            <el-button type="default" size="medium" @click="close">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </basic-container>
    
  </div>
</template>

<script>
  import selImgPhotogallery from '@/components/pds/common/sel_img_photogallery';
  import { fetchList as staffListApi } from '@/api/crm/marketemployee';
  import { fetchDict } from '@/util/util';
  import { getReportInfo } from '@/api/crm/workreport';
  import { updateReport } from '@/api/crm/workreport';
  import { getLocation } from '@/const/common/tool';
  import Vue from 'vue';
  import AvueMap from 'avue-plugin-map';
  import { mapGetters } from 'vuex';
  export default {
    data() {
      return {
        categoryForm:{
            photoList: [],
        },
        sleImg: false,
        staffList: [],
        reportTypeList: [],
        position:{
          formattedAddress:'',
          addressComponent:{
            city:'',
            district:'',
            province:'',
            township:''
          },
          longitude:'',
          latitude:''
        },
        //初始化坐标
        lnglat: {
          P: 29.563761,
          R: 106.55046400000003,
        },
      }
    },
    computed: {
      ...mapGetters(['tagWel', 'tagList', 'tag', 'website']),
      tagLen() {
        return this.tagList.length || 0
      }
    },
    components: {  selImgPhotogallery },
    created(){
        Vue.use(AvueMap);
        fetchDict('scm_work_report', this.reportTypeList);
        this.getStaffList();
        this.getReportDetail();
    },
    methods: {
      async getReportDetail() {
        let query = {
            reportSn: JSON.parse(this.$route.query.res).reportSn,
        };
        let res = await getReportInfo(query);
        this.categoryForm = res.data.data;
        let location = getLocation(this.categoryForm.reportLocation);
        this.lnglat = {P: location[1], R: location[0]};
        this.position.formattedAddress = this.categoryForm.reportAddress;
        this.position.longitude = location[0];
        this.position.latitude = location[1];
      },
      async remoteStaffList(name) {
        let query = {};
        if(name) query.name = name;
        let res = await staffListApi(Object.assign(query, {current: 1, size: 50}));
        this.staffList = res.data.data.records;
      },
      async getStaffList() {
        let res = await staffListApi({current: 1, size: 100});
        this.staffList = res.data.data.records;
      },
      // 删除el-upload的图片
      handlePictureRemove(index) {
        if(this.imgType == 1){
          this.categoryForm.bizLicense = '';
        }else{
         this.categoryForm.photoList.splice(index,1)
        }
      },
      changePosition(data) {
        this.categoryForm.reportAddress = data.formattedAddress;
      },
      sel_img(){
        this.sleImg = true;
      },
      onSelectImgs(imgs){
        imgs.forEach((item)=>{
          this.categoryForm.photoList.push({id:item.id,picUrl:item.picUrl});
        })
        this.sleImg = false;
      },
      save() {
        let params = this.categoryForm;
        params.reportLocation = this.position.longitude + ',' + this.position.latitude;
        updateReport(params).then(response => {
          if(response.data.code == 0){
            this.$message.success('创建成功')
            let _this = this;
            setTimeout(function () {
              _this.close();
            },2000)
          }else{
            this.$message.success(response.data.msg)
          }
        })
      },
      close(){
        var str_before = location.href.split('#')[1];
        let { tag, key } = this.findTag(str_before);
        this.$store.commit('DEL_TAG', tag);
        this.$router.go(-1);
      },
      findTag(value) {
        let tag, key
        this.tagList.map((item, index) => {
          if (item.value === value) {
            tag = item
            key = index
          }
        })
        return { tag: tag, key: key }
      }
    }
  }
</script>

<style scoped lang="scss">
  .crumbs{
    margin: 20px 10px;
  }
  .table {
    width: 100%;
    font-size: 14px;
    background-color: #fff;
    text-align: left;
    border-top: 1px solid #e7e7eb;
    border-bottom: 1px solid #e7e7eb;
    border-spacing: 0;
  }
  .thumbUrl{
    width: 150px;
    height: 150px;
    position: relative;
    line-height: 146px;
    float: left;
    margin-right: 15px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #c0ccda;
    text-align: center;
  }
  .thumbUrl img{
    width: 100%;
    height: 100%;
  }
  .thumbUrl .hover_show{
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0,0,0,0.5);
    width: 100%;
    height: 100%;
    display: none;
  }
  .thumbUrl:hover .hover_show{
    display: block;
  }
  .thumbUrl .hover_show i{
    color: #fff;
    margin: 0 10px;
    cursor: pointer;
  }
  .select_img{
    width: 148px;
    height: 148px;
    line-height: 146px;
    border: 1px dashed #c0ccda;
    background-color: #fbfdff;
    font-size: 28px;
    color: #8c939d;
    cursor: pointer;
    border-radius: 6px;
    text-align: center;
  }
</style>
